/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.scenic.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.model.ShopVo;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.scenic.entity.ScenicSpot;
import com.rzico.scenic.model.ScenicSpotVo;
import com.rzico.scenic.service.ScenicFavoritesService;
import com.rzico.scenic.service.ScenicSpotService;
import com.rzico.util.AreaUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 旅游景区控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "旅游景区接口")
@RestController
@RequestMapping("/scenicSpot")
public class ScenicSpotController extends BaseController {

    @Autowired
    private ScenicSpotService scenicSpotService;

    @Autowired
    private ScenicFavoritesService scenicFavoritesService;

    @Autowired
    private MemberService memberService;
    /**
     * 分页查询旅游景区
     *
     * @return
    */
    @ApiOperation("分页查询旅游景区")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lat", value = "纬度", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "经度", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "地区id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "搜索内容", dataType = "String", paramType = "query")
    })
    public CommResult<ScenicSpot> list(Double lat, Double lng, String areaId, String keyword, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (StringUtils.isNotEmpty(areaId)) {
            if (areaId.endsWith("00")) {
                AreaUtils.encodeMap(Long.parseLong(areaId),params);
            }else {
                params.put("areaId", areaId);
            }
        }
        if (StringUtils.isNotEmpty(keyword)) {
            params.put("keyword", keyword);
        }

         List<ScenicSpotVo> list = scenicSpotService.selectLbsList(params, lat ,lng);

        Integer startRow = (pageable.getPageNum()-1)*pageable.getPageSize();
        Integer endRow = startRow+pageable.getPageSize();
        if (endRow>list.size()) {
            endRow = list.size();
        }
        PageResult<ScenicSpotVo> pageResult = null;
        if (list.size()>0) {
            if (startRow<endRow) {
                pageResult = new PageResult<ScenicSpotVo>(list.subList(startRow, endRow), list.size(), pageable);
            } else {
                pageResult = new PageResult<ScenicSpotVo>(new ArrayList<ScenicSpotVo>(), list.size(), pageable);
            }
        } else {
            pageResult = new PageResult<ScenicSpotVo>(list, list.size(), pageable);
        }

        return CommResult.success(pageResult);
    }

    /**
     * 查询单条旅游景区
     *
     * @return
    */
    @ApiOperation("查询单条旅游景区")
    @GetMapping("/find/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "景区ID", dataType = "String", paramType = "query")
    })
    public CommResult<ScenicSpot> find(@PathVariable String id) {
        ScenicSpot result = scenicSpotService.findById(id);
        Map<String,Object> data = new HashMap<>();
        data.put("scenicSpot",result);
        Member member = memberService.getCurrent();
        Map<String,Object> params = null;
        boolean isAlaud = false;
        if (member != null && member.getId() !=null && member.getId() > 0) {
            params = new HashMap<>();
            params.put("scenicSpotId", id);
            params.put("memberId", member.getId());
            isAlaud = scenicFavoritesService.getIsFavorite(params);
        }
        data.put("isAlaud", isAlaud);
        return CommResult.success(data);
    }
}
